package org.reliablesource.prism.gef.editor.commands;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.reliablesource.prism.core.IPrismAnnotation;
import org.reliablesource.prism.gef.editor.model.PointerLocation;
import org.reliablesource.prism.gef.editor.model.ScreenAdapter;
import org.reliablesource.prism.gef.editor.parts.ScreenEditPart;

/**
 * 
 * StickyNoteSetConstraintCommand.java Copyright (c) 2006, Reliable Source, Inc.
 * All Rights Reserved
 * 
 * 
 * Created on: Apr 11, 2006
 * 
 * @author Dennis Park <a
 *         href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 * 
 */

public class PointerLocationSetConstraintCommand extends Command {
	/** Stores the new size and location. */
	private final Rectangle newLocation;

	/** Stores the old size and location. */
	private Rectangle oldLocation;

	/** Shape to manipulate. */
	private final PointerLocation pointerLocation;

	private final ChangeBoundsRequest request;
	private final ScreenEditPart parent;
	/**
	 * Create a command that can resize and/or move a pointerLocation.
	 * 
	 * @param _pointerLocation
	 *            the pointerLocation to manipulate
	 * @param req
	 *            the move and resize request
	 * @param _newLocation
	 *            the new size and location
	 * @throws IllegalArgumentException
	 *             if any of the parameters is null
	 */
	public PointerLocationSetConstraintCommand(
			final PointerLocation _pointerLocation, final ChangeBoundsRequest _request,
			Rectangle _newLocation, EditPart _parent) {
		if (_pointerLocation == null || _newLocation == null
				|| _request == null || _parent == null) {
			throw new IllegalArgumentException();
		}
		this.pointerLocation = _pointerLocation;
		this.newLocation = _newLocation.getCopy();
		this.request = _request;
		this.parent = (ScreenEditPart) _parent;
		setLabel("move");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		Object type = request.getType();
		
		// Make sure the Request is of a type that is supported
		return (RequestConstants.REQ_MOVE.equals(type) || RequestConstants.REQ_MOVE_CHILDREN
				.equals(type));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		oldLocation = new Rectangle(pointerLocation.getLocation().x,
				pointerLocation.getLocation().y,
				pointerLocation.getSize().width,
				pointerLocation.getSize().height);
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		pointerLocation.setLocation(newLocation.getLocation());
		((ScreenAdapter)parent.getModel()).childPropertyChange(pointerLocation, IPrismAnnotation.LOCATION, newLocation.getLocation());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		pointerLocation.setLocation(oldLocation.getLocation());
		((ScreenAdapter)parent.getModel()).childPropertyChange(pointerLocation, IPrismAnnotation.LOCATION, oldLocation.getLocation());
	}
}
